﻿using BuyWithoutLose.Models;
using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Controllers
{
    [AllowAnonymous]
    public class HomeController : Controller
    {

        private readonly ILogger<HomeController> _logger;
        private readonly IRepository<Product, int> _productRepostitory;
        private readonly IRepository<ProductType, int> _productTypeRepostitory;
        private readonly IRepository<ProductPic, int> _productPicRepostitory;
        private readonly IRepository<Position, int> _positionRepostitory;
        private readonly IRepository<ResPosition, int> _resPositionRepostitory;
        private readonly BuyWithoutLoseMoneyDBContext db;
        public HomeController(ILogger<HomeController> logger, 
            IRepository<Product, int> productRepository,
            IRepository<ProductType, int> productTypeRepository,
            IRepository<ProductPic, int> productPicRepostitory,
            IRepository<Position, int> positionRepostitory,
            IRepository<ResPosition, int> resPositionRepostitory, 
            BuyWithoutLoseMoneyDBContext buyWithoutLoseMoneyDBContext

            )
        {
            _logger = logger;
            _productRepostitory = productRepository;
            _productTypeRepostitory = productTypeRepository;
            _productPicRepostitory = productPicRepostitory;
            _positionRepostitory = positionRepostitory;
            _resPositionRepostitory= resPositionRepostitory;
            db = buyWithoutLoseMoneyDBContext;
        }
        public async Task<IActionResult> Index()
        {
            var pro = await _productRepostitory.GetAllListAsync();
            var proPic = await _productPicRepostitory.GetAllListAsync();
            ViewBag.ProPic = proPic;//将产品图片列表传递到视图中
            var proType = await _productTypeRepostitory.GetAllListAsync();
            ViewBag.ProType = proType;//将产品图片类型传递到视图中
            var postion = _resPositionRepostitory.GetAllList();
            ViewBag.Position = postion;//将位置资源传递到视图中
            return View(pro);//通过强类型传值到页面中
        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

    }
}
